package com.gears_software.sxdc.dccx.ui.fragment;

import android.support.v4.app.Fragment;
import android.view.View;
import android.widget.ListView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.gears_software.sxdc.dccx.R;
import com.gears_software.sxdc.dccx.application.DCCXApplication;
import com.gears_software.sxdc.dccx.data.adapter.VouchersAdapter;
import com.gears_software.sxdc.dccx.data.finder.IVouchersFinder;
import com.gears_software.sxdc.dccx.data.finder.util.GearsErrorHandler;
import com.gears_software.sxdc.dccx.data.model.ResultModel;
import com.gears_software.sxdc.dccx.data.model.VouchersModel;
import com.gears_software.sxdc.dccx.ui.util.MaterialDialogProgress;
import com.sdsmdg.tastytoast.TastyToast;

import org.androidannotations.annotations.AfterViews;
import org.androidannotations.annotations.App;
import org.androidannotations.annotations.Background;
import org.androidannotations.annotations.Bean;
import org.androidannotations.annotations.EFragment;
import org.androidannotations.annotations.UiThread;
import org.androidannotations.annotations.ViewById;
import org.androidannotations.rest.spring.annotations.RestService;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by chezhenzhen on 2017/11/22.
 */

@EFragment(R.layout.fragment_usevouchers)
public class UseVouchersFragment extends Fragment {
    @App
    DCCXApplication app;
    @RestService
    IVouchersFinder vouchersFinder;
    @Bean
    GearsErrorHandler errorHandler;
    @ViewById
    ListView list_use;
    @ViewById
    TextView txt_nodata_icon;
    @ViewById
    RelativeLayout re_nodata;
    List<VouchersModel> vouchersModelList = new ArrayList<VouchersModel>();
    String statusid;
    VouchersAdapter adapter;
    MaterialDialogProgress progress;

    @AfterViews
    void created(){
        vouchersFinder.setRestErrorHandler(errorHandler);
        statusid = "2";
        txt_nodata_icon.setTypeface(app.iconfont);
        progress = new MaterialDialogProgress(getActivity());
        progress.show("正在加载...");
        getList();
    }
    @Background
    void getList() {
        ResultModel<List<VouchersModel>> res = vouchersFinder.getVouchersList(statusid);
        showVouchersResult(res);
    }

    @UiThread
    void showVouchersResult(ResultModel<List<VouchersModel>> res) {
        progress.dismiss();
        if (res != null) {
            if (res.getErrno().equals("0")) {
                if (res.getData() != null) {
                    vouchersModelList = res.getData();
                    if (vouchersModelList.size() > 0) {
                        list_use.setVisibility(View.VISIBLE);
                        re_nodata.setVisibility(View.GONE);
                        adapter = new VouchersAdapter(getActivity(), vouchersModelList);
                        list_use.setAdapter(adapter);
                    }
                }
            } else {
                re_nodata.setVisibility(View.VISIBLE);
                list_use.setVisibility(View.GONE);
            }
        }
    }
}
